package com.library.dao;

import com.library.config.DBConnection;
import com.library.model.Manager;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ManagerDAO {
    public Manager getManagerByID(String managerId) {
        Manager manager = null;
        String sql = "SELECT * FROM manager WHERE manager_id =?";
        try (Connection conn = DBConnection.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql);
             ResultSet rs = pstmt.executeQuery()) {
            if (rs.next()) {
                manager = new Manager();
                manager.setManagerId(rs.getString("manager_id"));
                manager.setName(rs.getString("name"));
                manager.setTelephone(rs.getString("telephone"));
                manager.setPassword(rs.getString("password"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return manager;
    }

    public boolean updatePassword(Manager manager) {
        String sql = "UPDATE manager SET password =? WHERE manager_id =?";
        try (Connection conn = DBConnection.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, manager.getPassword());
            pstmt.setString(2, manager.getManagerId());
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
}